home *** CD-ROM | disk | FTP | other *** search
Java Source | 1996-10-06 | 2.5 KB | 81 lines |
- // ExampleServer.java
- // Simple Server Application
- import java.net.*;
- import java.io.*;
- import java.util.*;
-
- class ExampleServer{
- public final static int PORT = 4130;
-
- public static void main(String[] args){
- ServerSocket server_socket = null;
- Socket client_socket = null;
- DataInputStream in = null;
- DataOutputStream out = null;
- float x = 0.0f, y = 0.0f, z = 0.0f, r = 0.0f;
-
- System.out.println("Start server: " + PORT);
-
- // open socket on PORT
- try{
- server_socket = new ServerSocket(PORT);
- } catch(IOException e){
- System.out.println("Could not create socket on: " + PORT + ", " + e);
- System.exit(1);
- }
-
- System.out.println("Socket created: " + PORT);
- System.out.println("Waiting for client...");
-
- // accept client's request
- try{
- client_socket = server_socket.accept();
- } catch(IOException e) {
- System.out.println("Accept failed: " + PORT + ", " + e);
- System.exit(1);
- }
-
- System.out.println("Connection established: "
- + client_socket.getInetAddress());
- System.out.println("Open input/output stream...");
-
- try{
- in = new DataInputStream(client_socket.getInputStream());
- out = new DataOutputStream(client_socket.getOutputStream());
- } catch (IOException e) {
- System.out.println("Could not create input/output stream on: "
- + PORT + ", " + e);
- System.exit(1);
- }
-
- while(true){
- System.out.println("Reading data from client...");
- try {
- r = in.readFloat();
-
- x = in.readFloat();
- y = in.readFloat();
- z = in.readFloat();
- System.out.println(" rotation: " + r + " position: " + x + "," + y + "," + z);
- } catch (IOException e) {
- System.out.println("Could not read data.");
- System.exit(1);
- }
- try {
- // caliculate a new position
- x -= Math.sin(r);
- z -= Math.cos(r);
-
- out.writeFloat(x);
- out.writeFloat(y);
- out.writeFloat(z);
- System.out.println(" Sending new position to client: "
- + x + "," + y + "," + z);
- } catch (IOException e) {
- System.out.println("Could not write data");
- System.exit(1);
- }
- }
- }
- }
-